package com.example.demo.service.imply;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.annotation.AutoFill;
import com.example.demo.enumeration.OperationType;
import com.example.demo.mapper.ComponentMapper;
import com.example.demo.mapper.OutboundMapper;
import com.example.demo.mapper.StorageMapper;
import com.example.demo.pojo.PublicFields;
import com.example.demo.pojo.dto.InventoryDTO;
import com.example.demo.pojo.dto.OutboundDTO;
import com.example.demo.pojo.entity.*;
import com.example.demo.pojo.query.OutboundQuery;
import com.example.demo.pojo.vo.OutboundVO;
import com.example.demo.pojo.vo.PageVO;
import com.example.demo.service.IOutboundService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class IOutboundServiceImply extends ServiceImpl<OutboundMapper, Outbound> implements IOutboundService {
    private final IInventoryServiceImply inventoryService;
    private final ComponentMapper componentMapper;
    private final StorageMapper storageMapper;
    @Override
    @AutoFill(OperationType.INSERT)
    public int addOutbound(OutboundDTO outboundDTO, PublicFields publicFields) {
        Outbound outbound = new Outbound();
        BeanUtils.copyProperties(outboundDTO, outbound);
        BeanUtils.copyProperties(publicFields, outbound);
        int deleteNumber=outbound.getOutQuantity();
        Inventory inventory=inventoryService.selectInventory(outbound.getComponentId(),outbound.getStorageId());
        int old_number=inventory.getNumber();
        if(old_number-deleteNumber<0){
            return -1;
        }
        InventoryDTO inventoryDTO=new InventoryDTO();
        inventoryDTO.setNumber(old_number-deleteNumber);
        inventoryDTO.setStorageId(outbound.getStorageId());
        inventoryDTO.setComponentId(outbound.getComponentId());
        //更新数量
        inventoryService.updateInventory(inventoryDTO,PublicFields.newInstance());
        double price=componentMapper.selectById(outboundDTO.getComponentId()).getPrice()*outboundDTO.getOutQuantity();
        outbound.setPrice(price);
        baseMapper.insert(outbound);
        return 1;
    }

    @Override
    public void deleteOutbound(List<Long> ids) {
        baseMapper.deleteBatchIds(ids);
    }

    @Override
    public PageVO<OutboundVO> queryOutboundPage(OutboundQuery outboundQuery) {
        System.out.println("------------------------");
        System.out.println(outboundQuery);
        Page<Outbound> p = Page.of(outboundQuery.getPageNo(), outboundQuery.getPageSize());
        p.addOrder(OrderItem.desc("update_time"));
        PageVO<OutboundVO> outboundVOs = new PageVO<>();
        Page<Outbound> page = lambdaQuery()
                .like(StringUtils.hasText(outboundQuery.getStorageId()), Outbound::getStorageId, outboundQuery.getStorageId())
                .like(StringUtils.hasText(outboundQuery.getComponentId()), Outbound::getComponentId, outboundQuery.getComponentId())
                .page(p);
        outboundVOs.setPages(p.getPages());
        outboundVOs.setTotal(p.getTotal());
        List<Outbound> records = p.getRecords();
        List<OutboundVO> OutboundVOList = records.stream().map(outbound -> {
            OutboundVO outboundVO = new OutboundVO();
            BeanUtils.copyProperties(outbound, outboundVO);
            Component component=componentMapper.selectById(outbound.getComponentId());
            Storage storage=storageMapper.selectById(outbound.getStorageId());
            outboundVO.setComponentName(component.getComponentName());
            outboundVO.setStorageName(storage.getStorageName());
            return outboundVO;
        }).collect(Collectors.toList());
        outboundVOs.setList(OutboundVOList);
        return outboundVOs;
    }
}
